<template>
  <div>
    <!-- 弹出层的头部区域 -->
    <van-nav-bar title="频道管理">
      <template #right>
        <van-icon name="cross" size="14" color="white" @click="$emit('closePop')"/>
      </template>
    </van-nav-bar>

    <!-- 我的频道 -->
    <div class="my-channel-box">
      <div class="channel-title">
        <span @click="isedit = !isedit"
          >已添加频道: {{ isedit ? "点击移除频道" : "点击进入频道" }}</span
        >
        <span @click="isedit = !isedit">{{ isedit ? "完成" : "编辑" }}</span>
      </div>
      <!-- 我的频道列表 -->
      <van-row type="flex">
        <van-col span="6" v-for="obj,index in userchannelList" :key="obj.id" >
          <div class="channel-item van-hairline--surround"  @click="removeFn(obj,index)">
            {{ obj.name }}
            <!-- 删除的徽标 -->
            <van-badge
              color="transparent"
              class="cross-badge"
              v-if="isedit && obj.id !== 0"

           >
              <template #content>
                <van-icon
                  name="cross"
                  class="badge-icon"
                  color="#cfcfcf"
                  size="0.32rem"
                />
              </template>
            </van-badge>
          </div>
        </van-col>
      </van-row>
    </div>

    <!-- 更多频道 -->
    <div class="more-channel-box">
      <div class="channel-title">
        <span>点击添加更多频道：</span>
      </div>
      <!-- 更多频道列表 -->
      <van-row type="flex">
        <van-col span="6" v-for="obj,index in unchannelList" :key="index" @click="addbiao(obj,index)">
          <div class="channel-item van-hairline--surround">
            {{ obj.name }}
          </div>
        </van-col>
      </van-row>
    </div>
  </div>
</template>

<script>
import { allChannelListAPI, updateChannelAPI } from '@/api'
export default {
  props: {
    userchannelList: {
      type: Array,
      default () {
        return []
      }
    }
  },
  data () {
    return {
      isedit: false,
      allChannelList: []
    }
  },
  async created () {
    const res = await allChannelListAPI()
    this.allChannelList = res.data.data.channels
  },
  computed: {
    // 没有选中的数据是所有的数据(allChannelListAPI)减去选中的数据(userchannelList)
    unchannelList () {
      // 用filter方法筛选
      return this.allChannelList.filter((obj) => {
        const index = this.userchannelList.findIndex((item) => {
          // 查找两组数据中的id,如果两组的id相同那么久return一个index
          return item.id === obj.id
        })
        // 如果index不等于-1也就是说有两个相等的id,再或者说有两个相同的数据
        if (index !== -1) {
          return false
          // return false 也就是说不把重复的数据返回出去
        } else {
          return true
          // 否则,如果不是重复的数据那么就返回出去,然后再遍历unchannelList进行没有选中的数据的渲染
        }
      })
    }
  },
  methods: {
    addbiao (obj) {
      this.userchannelList.push(obj)
      this.updatefn()
      //   创建一个新的arr数据
    },
    // 添加和删除之后都要更新数据所以统一封装
    updatefn () {
      const arr = []
      // 把userchannelList里面的对象遍历筛选出来赋值到新的arr数组上,这样操作新数组旧的数组就不会受影响
      this.userchannelList.forEach(obj => {
        const newobj = { ...obj }
        arr.push(newobj)
      })
      //   把推荐删掉因为一会传值更新的时候后台规定不需要推荐
      //   删除推荐的方式
      const indexT = arr.findIndex((obj) => {
        return obj.name === '推荐'
      })
      arr.splice(indexT, 1)
      // 循环遍历arr数据,删除name属性,加一个后台需要的seq属性,delete是内置的删除方法
      arr.forEach((obj, index) => {
        delete obj.name
        // 后天要求seq属性为index(索引加1)
        obj.seq = index + 1
      })
      const res = updateChannelAPI({
        channels: arr
      })
      console.log(res)
    },
    removeFn (obj, index) {
      if (obj.name !== '推荐' && this.isedit === true) {
        this.userchannelList.splice(index, 1)
        this.updatefn()
      } else {
        //   子传父关闭弹出层
        this.$emit('closePop')
        // 子传父,点击频道标签改变相应的频道
        this.$emit('channelId', obj)
      }
    }
  }
}
</script>

<style scoped lang="less">
.van-popup,
.popup-container {
  background-color: transparent;
  height: 100%;
  width: 100%;
}

.popup-container {
  display: flex;
  flex-direction: column;
}

.pop-header {
  height: 90px;
  background-color: #007bff;
  color: white;
  text-align: center;
  font-size: 14px;
  position: relative;
  .title {
    width: 100%;
    position: absolute;
    bottom: 15px;
  }
}

.pop-body {
  flex: 1;
  overflow: scroll;
  padding: 8px;
  background-color: white;
}

.my-channel-box,
.more-channel-box {
  .channel-title {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    line-height: 28px;
    padding: 0 6px;
  }
}

.channel-item {
  font-size: 12px;
  text-align: center;
  line-height: 36px;
  background-color: #fafafa;
  margin: 5px;
}

/*删除的微标 */
.cross-badge {
  position: absolute;
  right: -3px;
  top: 0;
  border: none;
}

/*提示文字 */
.small_title {
  font-size: 12px;
  color: gray;
}
</style>
